<?php

//imgg.php/1001/200/0
//imgg.php/商品番号/画像width/画像height


//imgg.php?g=images/aa.jpg&w=0&h=100
//imgg.php/商品番号/画像width/画像height

$catch_url = "images/catche/";
$none_file_url = "images/none.jpg";

$url  = isset($_GET['img'])?$_GET['img'] : $none_file_url ;
if (!file_exists($url) ){
	$url =  $none_file_url;
}

$size = isset($_GET['s'])?$_GET['s'] :"0-0-0" ;
$id   = isset($_GET['id'])?$_GET['id'] :"0000" ;

$urlarr =  explode(".", $url);
$jpgext = "jpg";
if (  isset($urlarr[1])   ){
	$jpgext = $urlarr[1] ;
}

$sizearr = explode("-", $size);
$urlarr =  explode(".", $url);

$w = isset($sizearr[0]) ? $sizearr[0] : 0;
$h = isset($sizearr[1]) ? $sizearr[1] : 0;
$t = isset($sizearr[2])?  $sizearr[2] : $jpgext ; 
/*
if ( empty($t) ){
	$t = $urlarr[1];
}
*/

$namearr = explode("/", $urlarr[0]);
$imgname = $namearr[count($namearr) -1];
$newimage = $catch_url.$imgname."-{$id}-{$w}-{$h}-hs";
//echo $newimage;

	
//resizeImage();
$newimageurl = $newimage.".".$jpgext;
if (file_exists($newimageurl) ){
	$im = imagecreatefromjpeg($newimageurl);
	header('Content-type: image/'.$jpgext);
	imagejpeg($im); 
	imagedestroy($im);
	exit;
}

$im = imagecreatefromjpeg($url);
header('Content-type: image/'.$jpgext);

 if(($w ) || ($h ))
  {
	resizeImage($im,$w,$h,$newimage,$jpgext);
  }else{
  	 imagejpeg($im); 
  }
imagedestroy($im); 

/*
//echo $newimage;
$image = new Imagick();
if (!file_exists($url) ){
		$image -> readImageBlob(file_get_contents("images/none.jpg"));
		//$image -> setImageFormat($t);
		if ($w !=0 || $h!=0) {
			$image -> thumbnailImage($w,$h);
		}
}else {
	if (file_exists($newimage) ){
	$image -> readImageBlob(file_get_contents($newimage));
}else{
	try{
		$image -> readImageBlob(file_get_contents($url));
		 if ($w !=0 || $h!=0) {
		 	//$image -> setImageFormat($t);
			$image -> thumbnailImage($w,$h);
			$image -> writeImage($newimage);
		 }
	}catch (Exception $e){
		$image -> readImageBlob(file_get_contents("images/none.jpg"));
		//$image -> setImageFormat($t);
		$image -> thumbnailImage($w,$h);
	}
}
}

header('Content-type: image/'.$t);
echo $image;
*/
function resizeImage($im,$maxwidth,$maxheight,$name,$filetype)
{
    $pic_width = imagesx($im);
    $pic_height = imagesy($im);
	$resizewidth_tag = false; 
	

        if($maxwidth)
        {
            $widthratio = $maxwidth/$pic_width;
            $resizewidth_tag = true;
        }

        if($maxheight )
        {
            $heightratio = $maxheight/$pic_height;
            $resizeheight_tag = true;
        }

        if($resizewidth_tag && $resizeheight_tag)
        {
            if($widthratio<$heightratio)
                $ratio = $widthratio;
            else
                $ratio = $heightratio;
        }

        if($resizewidth_tag && !$resizeheight_tag)
            $ratio = $widthratio;
        if($resizeheight_tag && !$resizewidth_tag)
            $ratio = $heightratio;

        $newwidth = $pic_width * $ratio;
        $newheight = $pic_height * $ratio;

        if(function_exists("imagecopyresampled"))
        {
            $newim = imagecreatetruecolor($newwidth,$newheight);
           imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
        }
        else
        {
            $newim = imagecreate($newwidth,$newheight);
           imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
        }

        $name = $name.".".$filetype;
        if ($filetype == "jpg"){
	        imagejpeg($newim); 
	        imagejpeg($newim,$name);
        } else if ($filetype == "png"){
	        imagepng($newim); 
	        imagepng($newim,$name);
        }
       
        imagedestroy($newim);
         
}
?>